<?php

class SGN_String {

    private static $_lower = array('á', 'à', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ặ', 'ằ', 'ẳ', 'ẵ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ',
        'đ',
        'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ',
        'í', 'ì', 'ỉ', 'ĩ', 'ị',
        'ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ',
        'ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự',
        'ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ');
    private static $_upper = array('Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ặ', 'Ằ', 'Ẳ', 'Ẵ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ',
        'Đ',
        'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ',
        'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị',
        'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ',
        'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự',
        'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ');

    public static function lower($string) {
        $string = strtolower($string);
        $string = str_replace(self::$_upper, self::$_lower, $string);
        return $string;
    }
    public static function upper($string) {
        $string = strtoupper($string);
        $string = str_replace(self::$_lower, self::$_upper, $string);
        return $string;
    }
    
    public static function utf82ascii($str)
	{
		$chars = array(
			'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă'),
			'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê'),
			'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị'),
			'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ'),
			'u'	=>	array('ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư'),
			'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ'),
			'd'	=>	array('đ','Đ'),
		);
		foreach ($chars as $key => $arr) 
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str);
		return $str;
	}
	
	public static function getVNChars()
	{
	    $result = '';
	    $result .= "à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ";
	    $result .= "|è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ";
	    $result .= "|ì|í|ị|ỉ|ĩ";
	    $result .= "|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ";
	    $result .= "|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ";
	    $result .= "|ỳ|ý|ỵ|ỷ|ỹ";
	    $result .= "|đ";
	
	    $result .= "|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ";
	    $result .= "|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ";
	    $result .= "|Ì|Í|Ị|Ỉ|Ĩ";
	    $result .= "|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ";
	    $result .= "|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ";
	    $result .= "|Ỳ|Ý|Ỵ|Ỷ|Ỹ";
	    $result .= "|Đ";
	    return $result;
	}
	
	/**
	 * Check whether there are badword inside sentence
	 * */
	public static function hasBadWord($word) {
	    $search = explode('|', SGN_Application::getConfig('sgn', 'settings', 'badwords'));
	    $search[] = "'";
	    $search[] = '/';
	    $originLen = strlen($word);
	    $profileNameLen = strlen(str_replace($search, '', $word));
	    if(strlen($word) != $profileNameLen){
	        return true;
	    }
	    
	    if(strlen(strip_tags($word)) != $originLen){
	        return true;
	    }
	    
	    return false;
	}
	
	/**
	 * censor bad words of string
	 * @param string $censorCharacterString
	 * @param string $censorCharacter
	 * 
	 * @return string
	 */
	public static function censorBadWord($cencorString, $censorCharacter = '*') {
		$badWords = explode('|', SGN_Application::getConfig('sgn', 'settings', 'badwords'));
		foreach ($badWords as $badWord){
		    $censorCharacterString = '';
		    for ($i = 0; $i < strlen($badWord); $i++){
		    	$censorCharacterString .= $censorCharacter;
		    }
			$cencorString = str_replace($badWord, $censorCharacterString, $cencorString);
		}
		return $cencorString;
	}
	
	

}